<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.keyword" placeholder="关键词" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openEdit(0)"
                     v-has-permi="['admin:equipment:save']">添加
          </el-button>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-upload
              class="upload-demo"
              style="display:inline-block!important;"
              action
              accept=".xlsx, .xls"
              :before-upload="onBeforeUpload"
              :http-request="handleUploadForm"
              :headers="myHeaders"
              :show-file-list="false"
          >
            <el-button icon="el-icon-upload2" size="mini" type="success" v-has-permi="['admin:equipment:import']">导入
            </el-button>
          </el-upload>
        </el-form-item>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-download" size="mini" type="success" @click="download()">导入模板下载
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column align="center" label="ID" prop="id" width="50"></el-table-column>
        <el-table-column align="center" label="户号" prop="account" width="100"/>
        <el-table-column align="center" label="客户姓名" prop="userName" width="100"/>
        <el-table-column align="center" label="联系电话" prop="userPhone" width="100"/>
        <el-table-column align="center" label="详细地址" prop="address" min-width="150"/>
        <el-table-column align="center" label="燃气表编号" prop="gasNumber" min-width="80"/>
        <el-table-column align="center" label="安装日期" prop="installTime" min-width="80"/>
        <el-table-column align="center" label="燃气量" prop="gasQuantity" width="80"/>
        <el-table-column align="center" label="备注" prop="notes" min-width="150"/>
        <el-table-column align="center" label="创建时间" prop="createTime" min-width="80"/>
        <el-table-column align="center" label="操作" min-width="100" fixed="right">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEdit(1,scope.row)"
                       v-has-permi="['admin:equipment:save']">编辑
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:equipment:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[10,20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        :title="editDialog.id? '编辑':'新增'"
        destroy-on-close
        :close-on-click-modal="false"
        width="800px"
        top="30px">
      <edit
          v-if="editDialog.show"
          :is-create="editDialog.isCreate"
          :edit-data="editDialog.editDate"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit'
import {deleteRow, doImport, downloadImport, list} from "@/api/equipment";
import {checkPermi} from "@/utils/permission";
import {getToken} from "@/utils/auth";

export default {
  components: {edit},
  data() {
    return {
      listData: {
        list: [], count: 1
      },
      listPram: {
        keyword: '',
        page: 1,
        limit: 10,
      },
      editDialog: {
        show: false,
        isCreate: 0, // 0=创建，1=编辑
        editDate: {},
      },
      tableLoading: false,
      myHeaders: {"token": getToken()},
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    downloadImport,
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openEdit(isCreate, editData) {
      this.editDialog.editDate = editData;
      this.editDialog.isCreate = isCreate;
      this.editDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    resetQuery() {
      this.listPram.keyword = '';
      this.getList()
    },
    onBeforeUpload(file) {
      const isEXCEL = file.type === 'application/xls' || 'application/x-xls' || 'application/zip' || 'application/rar';
      const isLt1M = file.size / 1024 / 1024 < 100;

      if (!isEXCEL) {
        this.$message.error('上传文件只能是EXCEL格式!');
      }
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 100MB!');
      }
      return isEXCEL && isLt1M;
    },
    handleUploadForm(param) {
      const formData = new FormData();
      formData.append("file", param.file);
      let loading = this.$loading({
        lock: true,
        text: "上传中，请稍候...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      doImport(formData)
          .then((res) => {

            let msg = "";
            if (res) {
              msg = res;
              this.$message.error(msg);
            } else {
              msg = "导入成功";
              this.$message.success(msg);
            }
            loading.close();

            this.handleGetPointList()
          })
          .catch((res) => {
            loading.close();
          });
    },
    download() {
      downloadImport().then((res) => {
      });
    }
  }
}
</script>

<style scoped lang="scss">

</style>
